package com.fujin.gulimall.ssoclient.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @title: ssoClientController
 * @Author uIn
 * @Date: 2022/3/6 17:34
 * @Version 1.0
 */
@Controller
public class ssoClientController {

    @GetMapping("list")
    public String list(HttpSession session,
                       Model model,
                       @RequestParam(value = "token", required = false) String token){

        if (token != null){
            //通过token可以获取用户信息

            //这里zhangsan暂时写死，为了后面获取不为空
            session.setAttribute("loginUser","zhangsan");
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null){
            //如果session为空，跳转到登录页面
            //并将当前url拼接到请求参数
            return "redirect:" + "http://sso.server.com:8080/login.html"+"?redirect_url=http://sso.client.com:8081/list";
        }else {
            List<String> emps = new ArrayList<>();

            emps.add("张三");
            emps.add("李四");

            model.addAttribute("emps", emps);
            return "list";
        }
    }
}
